package com.example.springstatemachine.controller;

import com.example.springstatemachine.consant.DocumentEvents;
import com.example.springstatemachine.consant.DocumentStates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.statemachine.StateMachine;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/document")
public class DocumentController2 {

    private final StateMachine<DocumentStates, DocumentEvents> stateMachine;

    @Autowired
    public DocumentController2(StateMachine<DocumentStates, DocumentEvents> stateMachine) {
        this.stateMachine = stateMachine;
    }

    @GetMapping("/submit")
    public String submitDocument() {
        stateMachine.sendEvent(DocumentEvents.SUBMIT);
        return "Document submitted. Current state: " + stateMachine.getState().getId();
    }

    @GetMapping("/approve")
    public String approveDocument() {
        stateMachine.sendEvent(DocumentEvents.APPROVE);
        return "Document approved. Current state: " + stateMachine.getState().getId();
    }

    @GetMapping("/reject")
    public String rejectDocument() {
        stateMachine.sendEvent(DocumentEvents.REJECT);
        return "Document rejected. Current state: " + stateMachine.getState().getId();
    }

    @GetMapping("/publish")
    public String publishDocument() {
        stateMachine.sendEvent(DocumentEvents.PUBLISH);
        return "Document published. Current state: " + stateMachine.getState().getId();
    }
}